package testng.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import lombok.Data;
import org.testng.annotations.Test;

/**
 * @author lilh
 * @date 2020/12/7 下午4:16
 */
public class JsonPathTest {

    private static String json = "{ \"store\": {\n" +
            "    \"book\": [ \n" +
            "      { \"category\": \"reference\",\n" +
            "        \"author\": \"Nigel Rees\",\n" +
            "        \"title\": \"Sayings of the Century\",\n" +
            "        \"price\": 8.95\n" +
            "      },\n" +
            "      { \"category\": \"fiction\",\n" +
            "        \"author\": \"Evelyn Waugh\",\n" +
            "        \"title\": \"Sword of Honour\",\n" +
            "        \"price\": 12.99\n" +
            "      },\n" +
            "      { \"category\": \"fiction\",\n" +
            "        \"author\": \"Herman Melville\",\n" +
            "        \"title\": \"Moby Dick\",\n" +
            "        \"isbn\": \"0-553-21311-3\",\n" +
            "        \"price\": 8.99\n" +
            "      },\n" +
            "      { \"category\": \"fiction\",\n" +
            "        \"author\": \"J. R. R. Tolkien\",\n" +
            "        \"title\": \"The Lord of the Rings\",\n" +
            "        \"isbn\": \"0-395-19395-8\",\n" +
            "        \"price\": 22.99\n" +
            "      }\n" +
            "    ],\n" +
            "    \"bicycle\": {\n" +
            "      \"color\": \"red\",\n" +
            "      \"price\": 19.95\n" +
            "    }\n" +
            "  }\n" +
            "}";


    //@Test
    public void t1() {
        List<String> titless = JsonPath.read(json, "$.store.book[*].price");

        System.out.println("$.store.book.title \n " + titless);

        //System.out.println(JsonPath.read(json, "$.store.book[?(@.title=='Moby Dick')].author").toString());
        List<Book> books = JsonPath.read(json, "$.store.book[*]");
        System.out.println(books);
    }

    //@Test
    public void t2() {
        String json = "{\n" +
                "  \"success\": true,\n" +
                "  \"errCode\": 0,\n" +
                "  \"datas\": {\n" +
                "    \"list\": [\n" +
                "      {\n" +
                "        \"name\": \"语文\",\n" +
                "        \"id\": \"yuwen\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"数学\",\n" +
                "        \"id\": \"shuxue\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"英语\",\n" +
                "        \"id\": \"yingyu\"\n" +
                "      }\n" +
                "    ]\n" +
                "  },\n" +
                "  \"errMsg\": \"success\",\n" +
                "  \"pageSize\": 10,\n" +
                "  \"totalCount\": 0,\n" +
                "  \"pageNum\": 1\n" +
                "}";
        List<String> ids = JsonPath.read(json, "$.datas.list[*].id");
        System.out.println(ids);
        List<String> names = JsonPath.read(json, "$.datas.list[*].name");
        System.out.println(names);
    }

    @Data
    static class Book {
        private String category;
        private String author;
        private String title;
        private String price;
    }

    @Test
    public void testUpload() {
        String str = "['blau-big.png || test/20201230/9fcd3c48-918c-46e4-9ad1-f0f9a5c77988_blau-big.png','id-blue-light.png || test/20201230/6bffdf97-30c1-474f-a2c3-5fad195bb363_id-blue-light.png']";
        JSONArray array = JSONObject.parseArray(str);
        if (Objects.nonNull(array)) {
            List<Map<String, String>> fileList = new ArrayList<>();
            for (int i = 0; i < array.size(); i++) {
                String file = array.getString(i);
                if (Objects.nonNull(file) && file.contains(" || ")) {
                    String[] files = file.split(" \\|\\| ");
                    Map<String, String> fileMap = new HashMap<>(2);
                    fileMap.put(files[0], files[1]);
                    fileList.add(fileMap);
                }
            }
            System.out.println(fileList);
        }
    }

    @Test
    public void testSetValue() {
        String json = "{\"objs\" : [{\"obj\" : 1411455611975}]}";
        DocumentContext ext = JsonPath.parse(json);
        JsonPath p = JsonPath.compile("$.objs[0].obj");
        ext.set(p, 1L);
        String author = ext.jsonString();
        System.err.println(author);
    }
}
